package com.like.fragment;

import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.like.adapter.ArticleReplyAdapter;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoGetReplyReq;
import com.like.entity.InfoGetReplyResp;
import com.like.entity.MeetingDetailResp;
import com.like.hospitalworld.R;
import com.like.hospitalworld.VideoActivity;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_video_reply)
public class VideoReplyFragment extends BaseFragment {

    @ViewInject(R.id.reply_list)
    PullToRefreshListView mReplyList;

    private ArticleReplyAdapter mReplyAdapter;
    private MeetingDetailResp mMeetingDetail;
    private InfoService mInfoService;
    private int mCurrentPage = 0;
    private int mPageCount = 15;

    @Override
    protected void onCreateView() {
        super.onCreateView();
        initList();
        mInfoService = RetrofitUtil.getService(InfoService.class);
        getReplyList(false);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (m == null)
                return;
            getReplyList(false);
        }
    }

    private void initList() {
        mReplyList.setMode(PullToRefreshBase.Mode.BOTH);
        mReplyList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage = 0;
                getReplyList(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage += 1;
                getReplyList(true);
            }
        });
    }

    public void setMeetingDetail(MeetingDetailResp meetingDetail) {
        mMeetingDetail = meetingDetail;
    }

    public void refreshReply() {
        mCurrentPage = 0;
        getReplyList(false);
    }

    private void getReplyList(final boolean append) {
        InfoGetReplyReq req = new InfoGetReplyReq(mMeetingDetail.id, mCurrentPage, mPageCount);
        Call<GeneralResponse<InfoGetReplyResp>> call = mInfoService.getInfoReplyList(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoGetReplyResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoGetReplyResp>> call, Response<GeneralResponse<InfoGetReplyResp>> response) {
                mReplyList.onRefreshComplete();
                InfoGetReplyResp resp = response.body().data;
                List<InfoGetReplyResp.InformationComment> comments = resp.comments;
                if (mReplyAdapter == null) {
                    mReplyAdapter = new ArticleReplyAdapter(comments, m);
                    mReplyList.setAdapter(mReplyAdapter);
                } else
                    mReplyAdapter.update(comments, append);
            }

            @Override
            public void onFailure(Call<GeneralResponse<InfoGetReplyResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
